package breeze.pixel.weather.apps_util.utils;

import android.content.Context;
import android.speech.tts.TextToSpeech;

import breeze.pixel.weather.apps_util.utils.appsettings.AppSettings;

public class TTSUtils implements TextToSpeech.OnInitListener{
    public static TTSUtils ttsUtils;

    private final Context mContext;

    private final TextToSpeech textToSpeech;

    private final AppSettings appSettings;

    public TTSUtils(Context context){
        this.mContext = context;
        appSettings = AppSettings.getInstance(context);
        textToSpeech = new TextToSpeech(mContext, this);
        textToSpeech.setPitch(appSettings.getSpeakPitch());
        textToSpeech.setSpeechRate(appSettings.getSpeakSpeed());
    }

    public static TTSUtils getInstance(Context context){
        if (ttsUtils == null){
            ttsUtils = new TTSUtils(context);
        }
        return ttsUtils;
    }

    @Override
    public void onInit(int status) {
        if (status != TextToSpeech.SUCCESS){
            speak("语音出现错误");
        }
    }

    public void speak(String content){
        if (textToSpeech!=null){
            textToSpeech.speak(content, TextToSpeech.QUEUE_ADD,null,null);
        }
    }

    public void finish(){
        textToSpeech.stop();
        textToSpeech.shutdown();
    }

    public void setSpeed(float speed){
        textToSpeech.setSpeechRate(speed);
    }

    public void setPitch(float pitch){
        textToSpeech.setPitch(pitch);
    }

}
